#-*- coding:utf-8 -*-

import re
import urllib.request

def craw(url,page):
	html1 = urllib.request.urlopen(url).read()
	file = open("C:/Users/will.mao/Desktop/爬虫demo/实战/京东手机图片/1.html","wb")
	file.write(html1)
	html1 = str(html1)
	pattern1 = '<ul class="gl-warp clearfix">.+?<div class="clr">'
	result1 = re.compile(pattern1).findall(html1)
	result1 = result1[0]
	pattern2 = '<img width="220" height="220" data-img="1" (src|data-lazy-img)="//(.+?\.(jpg|png))">'
	imagelist = re.compile(pattern2).findall(html1)
	x = 1
	for imageurl in imagelist:
		print(imageurl[1])
		imagename="C:\\Users\\will.mao\\Desktop\\爬虫demo\\实战\\京东手机图片\\图片\\"+str(page)+"-"+str(x)+".jpg"
		imageurl = "http://"+imageurl[1]
		print(imageurl)
		try:
			urllib.request.urlretrieve(imageurl,filename=imagename)
		except urllib.error.URLError as e:
			if hasattr(e,"code"):
				x+=1
			if hasattr(e,"reason"):
				x+=1
		x+=1
for i in range(1,2):
	url = "https://list.jd.com/list.html?cat=9987,653,655&page="+str(i)
	craw(url,i)